package com.bhs.sansonglogistics.ui.mine;

import android.graphics.Color;
import android.os.Bundle;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.RevenueListBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.loper7.date_time_picker.DateTimeConfig;
import com.loper7.date_time_picker.dialog.CardDatePickerDialog;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

public class RevenueListFragment extends BaseFragment implements NetCallBack, View.OnClickListener {

    private TextView mTvGeneralIncome;
    private TextView mTvYearMonth;

    public static RevenueListFragment newInstance(long theSameMonth, int type) {
        RevenueListFragment fragment = new RevenueListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putLong("theSameMonth", theSameMonth);
        fragment.setArguments(bundle);
        return fragment;
    }

    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private int status;
    private int page = 1;
    private long theSameMonth;
    private BaseQuickAdapter<RevenueListBean.DataDTO.ListDTO, BaseViewHolder> mAdapter;

    @Override
    public int initLayout() {
        return R.layout.fragment_revenue_list;
    }

    @Override
    public void initView(View root) {
        status = getArguments().getInt("type");
        theSameMonth = getArguments().getLong("theSameMonth");
        mRefreshLayout = root.findViewById(R.id.refreshLayout);
        mRvList = root.findViewById(R.id.rv_list);
        mTvGeneralIncome = root.findViewById(R.id.tv_general_income);
        mTvYearMonth = root.findViewById(R.id.tv_year_month);
        mTvYearMonth.setText(DateUtils.getYearMonth(theSameMonth));
        mTvYearMonth.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mAdapter = new BaseQuickAdapter<RevenueListBean.DataDTO.ListDTO, BaseViewHolder>(R.layout.item_revenue) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, RevenueListBean.DataDTO.ListDTO item) {
                Glide.with(getContext()).load(item.getHeaderpic()).into((ImageView) helper.getView(R.id.iv_head));
                helper.setText(R.id.tv_title, item.getNickname());
                helper.setText(R.id.tv_time, DateUtils.getTime2(item.getTime()));
                helper.setText(R.id.tv_money, String.format("￥%s", item.getPay_to_logistics_money()));
                helper.setText(R.id.tv_type, item.getPay_method() == 1 ? "现付" : "到付");
                helper.setBackgroundRes(R.id.tv_type, item.getPay_method() == 1 ? R.drawable.bg_blue_4 : R.drawable.bg_yellow_4);
            }
        };

        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        loadData();

        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));
    }

    Map<String, Object> map = new ArrayMap<>();

    private void loadData() {
        map.put("stime", DateUtils.getMonthBegin(theSameMonth));
        map.put("etime", DateUtils.getMonthEnd(theSameMonth));
        map.put("jiesuan_status", status);
        map.put("page", page);
        map.put("limit", 10);
        networkRequest(netApi.my_income_list(map), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        RevenueListBean bean = new Gson().fromJson(json, RevenueListBean.class);
        mRefreshLayout.setRefreshing(false);
        if (bean.isStatus()) {
            mTvGeneralIncome.setText(String.format("￥%s", bean.getData().getJiesuan_money()));
            if (page == 1) {
                mAdapter.setNewData(bean.getData().getList());
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(bean.getData().getList());
            }
            if (MyUtils.isEmpty(bean.getData().getList())) {
                mAdapter.loadMoreEnd();
            }
        } else {
            ToastUtil.show(bean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }

    CardDatePickerDialog datePickerDialog;

    @Override
    public void onClick(View view) {
        if (datePickerDialog == null) {
            datePickerDialog = new CardDatePickerDialog.Builder(getContext())
                    .setTitle("选择时间")
                    .showBackNow(false)
                    .setDisplayType(DateTimeConfig.YEAR, DateTimeConfig.MONTH)
                    .showFocusDateInfo(false)
                    .setThemeColor(Color.parseColor("#485EF4"))
                    .setOnChoose("确定", aLong -> {
                        theSameMonth = aLong;
                        mTvYearMonth.setText(DateUtils.getYearMonth(aLong));
                        page = 1;
                        loadData();
                        return null;
                    }).build();
        }
        datePickerDialog.show();
    }
}
